package com.wy.controller.wy;

import com.wy.model.sys.Buildings;
import com.wy.model.sys.PageResult;
import com.wy.service.wy.BuildingService;
import com.wy.utils.BaseResponseInfo;
import com.wy.utils.ConstantData;
import com.wy.utils.HTTPStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 楼栋管理
 */
@RestController
public class BuildingController {

	private Logger log = LoggerFactory.getLogger(BuildingController.class);

	@Resource(name = "buildingServiceImpl")
	private BuildingService buildingService;

	/**
	 * 获取楼栋列表数据
	 * 
	 * @param pageSize
	 * @param page
	 * @return
	 */
	@GetMapping("/buildings")
	public PageResult buildingList(int pageSize, int page,String houseId,String buildingName) {
		PageResult pageResult = new PageResult();
		pageResult.setData(buildingService.buildingList(pageSize, page * pageSize));
		pageResult.setTotalCount(buildingService.buildingSize(pageSize,  page * pageSize));
		log.debug("The method is ending");
		return pageResult;
	}


	/**
	 * 获取楼栋列表组成键值对数据
	 * @return
	 */
	@GetMapping("/buildingAll")
	public BaseResponseInfo buildingAll() {
		BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
		baseResponseInfo.data = buildingService.buildingAll();
		log.debug("The method is ending");
		return baseResponseInfo;
	}


	/**
	 * 新建楼栋信息
	 * @param buildings
	 * @return
	 */
	@PostMapping("/buildings/building")
	public Buildings insertMenu(@RequestBody Buildings buildings) {
		buildingService.insertBuilding(buildings);
		log.debug("The method is ending");
		return buildings;
	}

	/**
	 * 修改楼栋信息
	 * @param buildings
	 * @param id
	 * @return
	 */
	@PutMapping("/buildings/{id}")
	public Buildings updateMenu(@RequestBody Buildings buildings, @PathVariable int id) {
		if (buildings.getId() == id) {
			buildingService.updateBuilding(buildings);
		}
		log.debug("The method is ending");
		return buildings;
	}

	/**
	 * 删除楼栋信息
	 * 
	 * @param groupId
	 * @return
	 */
	@DeleteMapping("/buildings")
	public Integer deleteMenus(@RequestBody Integer groupId) {
		buildingService.deleteBuilding(groupId);
		return groupId;
	}

	/**
	 * 添加楼栋信息（无登录验证）
	 * @param buildings
	 * @return
	 */
	@ResponseBody
	@PostMapping("/save_building")
	public BaseResponseInfo saveHouse(@RequestBody Buildings buildings) {
		BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			buildingService.insertBuilding(buildings);
			baseResponseInfo.code = HTTPStatus.OK;
			data.put("message", ConstantData.MSG_SUCCESS_INFO);
		} catch (Exception ex){
			ex.printStackTrace();
			baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
		}
		baseResponseInfo.data = data;
		return baseResponseInfo;
	}


	/**
	 * 查询楼栋信息（无验证）
	 * @param pageSize
	 * @param start
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/get_building_list")
	public BaseResponseInfo getBuildingList(@RequestParam("pageSize") int pageSize, @RequestParam("start")int start) {
		BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			data.put("houses", buildingService.buildingList(pageSize, (start -1)* pageSize));
			data.put("count", buildingService.buildingSize(pageSize, (start -1) * pageSize));
			baseResponseInfo.code = HTTPStatus.OK;
			data.put("message", ConstantData.MSG_SUCCESS_INFO);
		}catch(Exception ex){
			ex.printStackTrace();
			baseResponseInfo.code = HTTPStatus.INTERNAL_SERVER_ERROR;
		}
		baseResponseInfo.data = data;
		return baseResponseInfo;
	}
}
